﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.ships.cruise {
    import Engine.Helpers.*;
    import Classes.*;
    import Classes.sim.*;
    import Classes.actions.*;

    public class CruiseShipManager implements IGameWorldUpdateObserver {

        protected static const CHEERING_NPC_COUNT:int = 10;
        protected static const TOURIST_SPAWN_DELAY:int = 200;
        protected static const MAX_DOOBERS:int = 10;

        protected var m_cheeringNpcs:Array;
        protected var m_lastSpawnTime:Number = 0;
        protected var m_leftToSpawn:Array;

        public function CruiseShipManager(_arg1:GameWorld){
            this.m_cheeringNpcs = [];
            this.m_leftToSpawn = [];
            super();
            _arg1.addObserver(this);
        }
        public function initialize():void{
        }
        public function cleanUp():void{
            this.removeCheeringScene();
        }
        public function onGameLoaded(_arg1:Object):void{
            var _local4:String;
            var _local5:int;
            var _local6:int;
            var _local7:HarvestableShip;
            if (((!(_arg1.hasOwnProperty("featureData"))) || (!(_arg1.featureData.hasOwnProperty("cruiseShips"))))){
                return;
            };
            var _local2:* = _arg1.featureData.cruiseShips;
            var _local3:* = _local2.tourists;
            for (_local4 in _local3) {
                _local5 = int(_local3[_local4]);
                _local6 = int(String(_local4).substr(1));
                _local7 = (Global.world.getObjectById(_local6) as HarvestableShip);
                if (_local7 != null){
                    if (_local7.getState() != HarvestableResource.STATE_GROWN){
                        this.releasePeeps(_local5, _local7);
                    };
                };
            };
            this.checkCheeringScenes();
        }
        public function onResourceChange(_arg1:MapResource, _arg2:Vector3, _arg3:Vector3):void{
        }
        public function update(_arg1:Number):void{
            if (this.m_leftToSpawn.length > 0){
                _arg1 = (GlobalEngine.serverTime - this.m_lastSpawnTime);
                if (_arg1 > TOURIST_SPAWN_DELAY){
                    this.m_lastSpawnTime = GlobalEngine.serverTime;
                    this.spawnNextTourist();
                };
            };
        }
        public function checkCheeringScenes():void{
        }
        public function createCheeringScene():void{
        }
        public function isCheeringSceneActive():Boolean{
            return ((this.m_cheeringNpcs.length > 0));
        }
        public function removeCheeringScene():void{
            var _local1:Peep;
            for each (_local1 in this.m_cheeringNpcs) {
                _local1.actionQueue.removeAllStates();
                _local1.actionQueue.addActions(new ActionPlayAnimation(_local1, "cheer", Math.random()), new ActionEnableFreedom(_local1, true, true));
            };
            this.m_cheeringNpcs = [];
        }
        protected function createCheeringSceneAroundDockBuilding(_arg1:MapResource):void{
            var _local3:Vector3;
            var _local4:Peep;
            var _local2:int;
            while (_local2 < CHEERING_NPC_COUNT) {
                _local3 = this.createPointAroundDockEntrance(_arg1);
                _local4 = Global.world.citySim.npcManager.createPeepWalkerAtPosition(_local3, false);
                _local4.spawnSource = Peep.SOURCE_TOURBUS;
                _local4.isFreeAgent = false;
                _local4.actionQueue.addActions(new ActionPlayAnimation(_local4, "cheer", 10000));
                this.m_cheeringNpcs.push(_local4);
                _local2++;
            };
        }
        protected function createPointAroundDockEntrance(_arg1:MapResource):Vector3{
            var _local2:Vector3 = CruiseShipUtil.getRoadsideDockEntrance(_arg1);
            var _local3:Vector3 = new Vector3(((_local2.x + 3) + (Math.random() * 2)), (_local2.y + ((Math.random() - 0.5) * 5)));
            return (_local3);
        }
        public function releasePeeps(_arg1:int, _arg2:HarvestableShip):void{
            var _local3:int;
            var _local4:Array = new Array(_arg1);
            var _local5:Item = ((_arg2)!=null) ? _arg2.getItem() : null;
            _local3 = 0;
            while (_local3 < _arg1) {
                _local4[_local3] = new TouristDef(_arg2);
                _local3++;
            };
            if (((!((_local5 == null))) && (!((_local5.getRandomSpawnNpc() == null))))){
                _local3 = 0;
                while (_local3 < _arg1) {
                    (_local4[_local3] as TouristDef).name = _local5.getRandomSpawnNpc();
                    _local3++;
                };
            };
            this.m_leftToSpawn = this.m_leftToSpawn.concat(_local4);
        }
        protected function spawnNextTourist():void{
            var _local1:TouristDef = this.m_leftToSpawn.shift();
            var _local2:MapResource = CruiseShipUtil.getDockBuilding();
            var _local3:Vector3 = CruiseShipUtil.getRoadsideDockEntrance(_local2);
            var _local4:Peep = Global.world.citySim.npcManager.createPeepWalkerAtPosition(_local3, false, _local1.name);
            _local4.animation = "static";
            _local4.actionSelection = new CruisePeepActionSelection(_local4, _local2);
            _local4.performsAmbientActions = true;
            _local4.spawnSource = Peep.SOURCE_CRUISESHIP;
            var _local5:Vector3 = this.createPointAroundDockEntrance(_local2);
            _local4.actionQueue.addActions(new ActionNavigateBeeline(_local4, _local5));
            var _local6:Road = Global.world.citySim.roadManager.findClosestRoad(_local5);
            if (_local6 != null){
                _local4.actionQueue.addActions(new ActionNavigate(_local4, _local6, null).setFallbackAction(new ActionNavigateBeeline(_local4, _local6.getPosition())));
            };
        }

    }
}//package Modules.ships.cruise 

import Engine.Helpers.*;
import Classes.*;

class TouristDef {

    public var shipLocation:Vector3;
    public var name:String;

    public function TouristDef(_arg1:HarvestableShip){
        this.shipLocation = _arg1.getPosition();
    }
}
